package com.bw.util;

import lombok.Data;

import java.util.List;

@Data
public class PageResult<T> {
    /**
     * 当前页
     */
    private Integer currentPage;
    /**
     * 上一页
     */
    private Integer prevPage;
    /**
     * 总条数
     */
    private Integer totalNum;
    /**
     * 分页单位
     */
    private Integer pageSize;

    /**
     * 尾页
     */
    private Integer totalPage;
    /**
     * 下一页
     */
    private Integer nextPage;

    /**
     * 起始索引
     */
    private Integer startIndex;

    /**
     * 集合的数据
     */
    private List<T> data;

    public PageResult(Integer pageNum, Integer totalNum, Integer pageSize, List<T> data) {
        this.initCurrentPage(pageNum);
        this.totalNum = totalNum;
        this.pageSize = pageSize;
        this.initPrevPage();
        this.initTotalPage();
        this.initNextPage();
        this.initStartIndex();
        this.data = data;
    }

    //首条访问 值为1
    public void initCurrentPage(Integer pageNum) {
        this.currentPage = pageNum;
    }

    //上一页
    public void initPrevPage() {

        prevPage = currentPage == 1 ? 1 : currentPage - 1;
        if (prevPage <= 0){
            prevPage=1;
        }
    }

    //尾页
    public void initTotalPage() {
        totalPage = totalNum / pageSize + (totalNum % pageSize == 0 ? 0 : 1);
    }

    //下一页
    public void initNextPage() {
        nextPage = currentPage.equals(totalPage) ? totalPage : currentPage + 1;
    }

    //起始索引
    public void initStartIndex() {
        startIndex = (currentPage - 1) * pageSize;
    }

}
